﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class TableConfig : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();

        }
      
            

    }

    private void BindData()
    {
        gridList.DataSource = new Jumpton.SFA.Server.BusinessRule.System.TableConfigManager().LoadALL();
        gridList.DataBind();
    }

    /// <summary>
    /// 分页操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gridList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
       
        gridList.PageIndex = e.NewPageIndex;
        BindData();
        gridList.SelectedIndex = -1;
        gridList_SelectedIndexChanged(sender, e);
    }
    /// <summary>
    /// 保存数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!IsValid)
            return;
        if (gridList.SelectedIndex < 0)
            return;

        string[] types = new string[]{"文本型","整数型","小数型","日期型","是否型","选择型"};
        var  manager = new Jumpton.SFA.Server.BusinessRule.System.TableConfigManager();
        var record = manager.Load(int.Parse(gridList.SelectedDataKey.Value.ToString()));
        if (record == null)
            return;
      
        string LogContent = "";
        LogContent += string.Format("进行{0}表字段{1}配置，修改前为：标题{2}，状态:{3}，字段类型{4}。", 
            record.TableName,record.ColumnName , record.Title, (record.Enabled ? "启用":"关闭"),types[(int)record.Type] );
        

        record.Title = txtTitle.Text;
        record.Type = byte.Parse(drpType.SelectedValue);
        record.Enabled = chkEnabled.Checked;
        if("5".Equals(drpType.SelectedValue))
            record.ListItem = txtListItem.Text;
        LogContent += string.Format("修改后为:标题{0}，状态:{1}，字段类型{2}。", record.Title, (record.Enabled ? "启用" : "关闭"), types[(int)record.Type]);
        
        LogContent += "变更人：" + MyStateManager.Name;
         try
        {
            manager.Save(record);
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
              Jumpton.SFA.Server.BusinessRule.System.LogType.AUTOSYNC, this.MyStateManager.Name,
              this.MyStateManager.UserID, LogContent);
            BindData();

            gridList.SelectedIndex = -1;
        }
        catch (Exception ex)
        {
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
              Jumpton.SFA.Server.BusinessRule.System.LogType.GENERAL_EXCEPTION, this.MyStateManager.Name,
              this.MyStateManager.UserID, "冗余字段配置 - 异常:" + ex.Message);
            ShowMessage("冗余字段配置出现异常，请重新尝试");
            return;
        }
    }


    /// <summary>
    /// 拒绝操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        gridList.SelectedIndex = -1;
        gridList_SelectedIndexChanged(sender, e);
    }


    protected void gridList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (gridList.SelectedIndex < 0)
        {
            tdColumnName.InnerHtml = tdTableName.InnerHtml = "";
            txtTitle.Text = "";
            drpType.SelectedValue = "0";
            chkEnabled.Checked = true;
            return;
        }
        //显示该条记录
        var manager = new Jumpton.SFA.Server.BusinessRule.System.TableConfigManager();
        var record = manager.Load(int.Parse(gridList.SelectedDataKey.Value.ToString()));
        if (record == null)
            return;
        txtTitle.Text = record.Title;
        //tdTableName.InnerText = record.TableName;//HZJ注释于2010.3.31
        tdTableName.InnerText = record.Description;//HZJ增加于2010.3.31
        tdColumnName.InnerText = record.ColumnName;
        drpType.SelectedValue = record.Type.ToString();
        panelShowItem.Visible = "5".Equals(drpType.SelectedValue);
        chkEnabled.Checked = record.Enabled;
        txtListItem.Text = record.ListItem;
    }
    protected void gridList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.DataItem == null)
            return;
        var row = e.Row.DataItem as Jumpton.SFA.Server.Entity.TableConfig;

        if (row == null)
            return;
    
        switch (row.Type)
        {
            case 0:
                e.Row.Cells[3].Text = "文本型";
                break;
            case 1:
                e.Row.Cells[3].Text = "整数型";
                break;
            case 2:
                e.Row.Cells[3].Text = "小数型";
                break;
            case 3:
                e.Row.Cells[3].Text = "日期型";
                break;
            case 4:
                e.Row.Cells[3].Text = "是否型";
                break;
            case 5:
                e.Row.Cells[3].Text = "选择型";
                break;
        }

    }

    /// <summary>
    /// 选择输入项类型
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpType_SelectedIndexChanged(object sender, EventArgs e)
    {
        panelShowItem.Visible = "5".Equals(drpType.SelectedValue);
    }
}
